define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'scanwork/industry_config/index' + location.search,
                    add_url: 'scanwork/industry_config/add',
                    edit_url: 'scanwork/industry_config/edit',
                    del_url: 'scanwork/industry_config/del',
                    multi_url: 'scanwork/industry_config/multi',
                    table: 'scanwork_industry_config',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'industry_code', title: __('行业代码'), operate: 'LIKE'},
                        {field: 'industry_name', title: __('行业名称'), operate: 'LIKE'},
                        {field: 'description', title: __('行业描述'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), searchList: {"1":__('启用'),"0":__('禁用')}, formatter: Table.api.formatter.status},
                        {field: 'is_active', title: __('是否激活'), searchList: {"1":__('是'),"0":__('否')}, formatter: Table.api.formatter.normal},
                        {field: 'sort', title: __('排序')},
                        {field: 'createtime', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('更新时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        switch: function () {
            // 行业切换页面
            Form.api.bindevent($("#industry-switch-form"));
            
            // 表单提交处理
            $('#industry-switch-form').on('submit', function(e) {
                e.preventDefault();
                
                var form = $(this);
                var industryCode = form.find('select[name="industry_code"]').val();
                
                if (!industryCode) {
                    Toastr.error('请选择要切换的行业');
                    return;
                }
                
                Fast.api.ajax({
                    url: 'scanwork/industry_config/switch',
                    data: {industry_code: industryCode},
                    success: function(data, ret) {
                        Toastr.success('行业切换成功！');
                        setTimeout(function() {
                            location.reload();
                        }, 1500);
                    }
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});